Lesson 1 - Introduction to Python

# add table of contents # for jupyter notebook only, because for jupyter lab we have the table of contents int the left sidebar from jyquickhelper import add_notebook_menu add_notebook_menu(header="Table of contents", first_level=1, lasst_level=5)

We will be using the Jupyter notebook for many activities this module. Every notebook has an associated language called the "kernel". We will be using in the Python 3 kernel from the IPython project.

What is Python

Python is a programming language that has been under development for over 25 years [1] (Python was conceived in the late 1980s by Guido van Rossum).

His goal was to create a simple and intuitive computer language that is powerful and uses a computer syntax as understandable as English text.

Python is a general-purpose language, which means you can use it in any field to:

It is available on all computers, tablets and smartphones. Python is an interpreted language. This means that you can write Python commands and the computer can execute these instructions directly (using a Python interpreter)

The biggest companies use Python: Google, Facebook, Wikipedia, EDF, Airbus ...

Python is now one of the most widely used programming languages in the world: see for example

This Chapter will not cover everything in Python. If you would like, please consider the following resources:

Getting Started with Python:

Learning Python in Notebooks:

This is handy to always have available for reference:

Python Reference:

Statements

Python is an imperative language based on statements. That is, programs in Python consists of lines composed of statements. A statement can be:

https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator

Comments in Python

Comments in Python start with the hash character, #

Expressions

Numbers

operators +, -, * and / work just like in most other languages (for example, Pascal or C); parentheses () can be used for grouping

Strings

String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:

Strings can be concatenated (glued together) with the + operator, and repeated with *:

Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.

This only works with two literals though, not with variables or expressions:

If you want to concatenate variables or a variable and a literal, use +:

This feature is particularly useful when you want to break long strings:

Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one

Note that since -0 is the same as 0, negative indices start from -1

In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:

One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:

+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1

The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.

For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2.

Attempting to use an index that is too large will result in an error:

However, out of range slice indexes are handled gracefully when used for slicing:

Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:

The built-in function len() returns the length of a string:

Boolean

Lists and Dicts

Python has three very useful data structures built into the language:

see this example for more details

Function Calls

There are two ways to call functions in Python:

  1. by pre-defined infix operator name
  2. by function name, followed by parentheses

Infix operator name:

Print

Evaluating and display result as an Out, versus evaluating and printing result (side-effect).

Input, assignment

Special Values

Defining Functions

What happened? All functions return something, even if you don't specify it. If you don't specify a return value, then it will default to returning None. For more detail, see defining functions

Defining class

Classes provide a means of bundling data and functionality together. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.

class ClassName: . . .